#include "widget.h"
#include "ui_widget.h"
#include <QString>
#include <QMessageBox>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    tcpServer = new QTcpServer;
    tcpSocket = new QTcpSocket;

    connect(tcpServer,SIGNAL(newConnection()),this,SLOT(newConnect_slot()));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::newConnect_slot()
{
    tcpSocket = tcpServer->nextPendingConnection();
    connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readyRead_slot()));
}

void Widget::readyRead_slot()
{
    QString buf;
    buf = tcpSocket->readAll();
    ui->recv_box->appendPlainText(buf);
}
void Widget::on_open_clicked()
{
    tcpServer->listen(QHostAddress::Any,ui->portID->text().toUInt());
    QMessageBox::information(this,"提示","打开服务成功");
}

void Widget::on_close_clicked()
{
    tcpServer->close();
    QMessageBox::information(this,"提示","已关闭服务");
}

void Widget::on_send_clicked()
{
    tcpSocket->write(ui->send_box->text().toUtf8().data());
}
